package dataStructure.chapter06_树.part1二叉树.binarysearchtree1;


import java.util.Comparator;

/**
 * @author https://gitee.com/W_Jun
 * @date 2022-03-07 17:50
 * @Description  测试二叉搜索树
 */
public class TestBinarySearchTree {
    //定义比较器
    private static class PersonComparator implements Comparator<Person> {
        public int compare(Person e1, Person e2) {
            return e2.getAge() - e1.getAge();
        }
    }
    public static void main(String[] args) {
        Integer data[] = new Integer[] {7, 4, 9, 2, 5, 8, 11,3};
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }

        //不传递比较器，二叉搜索树compare方法会把每一个元素强制转换为可比较的元素
        BinarySearchTree<Person> bst2 = new BinarySearchTree<>();
        bst2.add(new Person(12));
        bst2.add(new Person(15));


        //传递一个比较器，二叉搜索树compare方法会按照比较器的规则进行比较
        BinarySearchTree<Person> bst3 = new BinarySearchTree<>(new PersonComparator());
        bst3.add(new Person(12));
        bst3.add(new Person(15));

        //通过匿名类的方式传递比较器
        BinarySearchTree<Person> bst31 = new BinarySearchTree<>(new Comparator<Person>() {
            @Override
            public int compare(Person e1, Person e2) {
                return e2.getAge() - e1.getAge();
            }
        });
    }
}
